package com.lori.netshop.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lori.netshop.service.ContentCatService;
import com.lori.netshop.util.NetshopResult;

@Controller
@RequestMapping("/content/category")
public class ContentCatController {
	@Autowired
	private ContentCatService contentCatServiceimp;
	@RequestMapping("/list")
	@ResponseBody
	public String queryByID(@RequestParam(value="id",defaultValue="0") long parentid){
		String json = contentCatServiceimp.queryByID(parentid);
		return json;
	}
	
	@RequestMapping("/create")
	@ResponseBody
	public NetshopResult save(@RequestParam(value="parentId") long id,String name){
		return contentCatServiceimp.save(id, name);
	}
	@RequestMapping("/update")
	@ResponseBody
	public String update(@RequestParam("id") long id,String name) throws JsonProcessingException{
		NetshopResult update = contentCatServiceimp.update(id, name);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(update);
		return json;
	}
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(@RequestParam("parentId") long parentId,long id) throws JsonProcessingException{
		NetshopResult result = contentCatServiceimp.delete(parentId, id);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(result);
		return json;
	}
}
